package com.mall.users.controller;

import com.mall.commons.utils.CommonResult;
import com.mall.commons.utils.Constants;
import com.mall.commons.utils.IpUtil;
import com.mall.commons.utils.JwtUtils;
import com.mall.model.domain.UmsAdmin;
import com.mall.model.domain.UmsRole;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.UmsAdminVo;
import com.mall.users.interceptor.UmsAdminHolder;
import com.mall.users.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminService umsAdminService;

    /**
     * 登录以后返回token
     * @param umsAdmin
     * @return
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody UmsAdmin umsAdmin, HttpServletRequest request) {
        log.info(umsAdmin.toString());
        //获取登录ip
        String ip = IpUtil.getIpAddr(request);
        String userAgent = IpUtil.getUserAgent(request);
        Map retMap = umsAdminService.login(umsAdmin,userAgent,ip);
        return CommonResult.success(retMap);
    }


    /**
     * 获取当前登录用户信息
     * @return
     */
    @GetMapping("/info")
    public CommonResult info(HttpServletRequest request,
                             @RequestParam(required = false) String name) {
        String username = UmsAdminHolder.getUsername();
        name = name == null ? username : name;
        UmsAdminVo vo = umsAdminService.info(name);
        return CommonResult.success(vo);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             String keyword) {
        CommonPage page = umsAdminService.list(pageNum,pageSize, keyword);
        return CommonResult.success(page);
    }

    /**
     * 登出功能
     * @return
     */
    @PostMapping("/logout")
    public CommonResult logout(HttpServletRequest request) {
        request.removeAttribute(Constants.TOKEN_PREFIX +Constants.TOKEN_AUTHORIZATION);
        return CommonResult.success(null);
    }

    /**
     * 刷新token
     * @return
     */
    @GetMapping("/refreshToken")
    public CommonResult refreshToken(HttpServletRequest request) {
        Map tokenMap = new HashMap();
        tokenMap.put("id", UmsAdminHolder.getUserId());
        tokenMap.put("username", UmsAdminHolder.getUsername());
        String refreshToken = JwtUtils.getToken(tokenMap);
        request.setAttribute(Constants.TOKEN_AUTHORIZATION,Constants.TOKEN_PREFIX + refreshToken);
        return CommonResult.success(null);
    }

    /**
     * 删除指定用户信息
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        umsAdminService.delete(id);
        return CommonResult.success(null);
    }

    /**
     * 用户注册
     * @param umsAdmin
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestBody UmsAdmin umsAdmin) {
        UmsAdmin umsAdminNew = umsAdminService.register(umsAdmin);
        return CommonResult.success(umsAdminNew);
    }

    /**
     * 给用户分配角色
     * @param roleIds
     * @param adminId
     * @return
     */
    @PostMapping("/role/update")
    public CommonResult updateRole(@RequestParam List<Long> roleIds, Long adminId) {
        log.info("roleIds: {},adminId: {}",roleIds,adminId);
        Integer count = umsAdminService.updateRole(adminId,roleIds);
        return CommonResult.success(count);
    }


    /**
     * 获取指定用户的角色
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public CommonResult getRole(@PathVariable Long adminId) {
        List<UmsRole> umsRoleList = umsAdminService.getRole(adminId);
        return CommonResult.success(umsRoleList);
    }

    /**
     * 获取指定用户的信息
     * @return
     */
    @PostMapping("/{id}")
    public CommonResult findById(@PathVariable Long id) {
        log.info("id: {}",id);
        UmsAdmin umsAdmin = umsAdminService.findById(id);
        return CommonResult.success(umsAdmin);
    }

    /**
     * 修改指定用户信息
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id,@RequestBody UmsAdmin umsAdmin) {
        log.info("id: {}",id);
        umsAdminService.update(umsAdmin);
        return CommonResult.success(1);
    }

    /**
     * 修改指定用户密码
     * @return
     */
    @PostMapping("/updatePassword")
    public CommonResult updatePassword(@RequestBody Map map) {
        log.info("map: {}",map);
        String newPassword = (String) map.get("newPassword");
        String oldPassword = (String) map.get("oldPassword");
        String username = (String) map.get("username");
        umsAdminService.updatePassword(username, oldPassword, newPassword);
        return CommonResult.success(1);
    }


    /**
     * 修改帐号状态
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Long id, Integer status) {
        umsAdminService.updateStatus(id,status);
        return CommonResult.success(1);
    }
}
